package com.github.uiautomator;

import android.graphics.Point;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import com.github.uiautomator.compat.WindowManagerWrapper;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Console {
    final static String PROCESS_NAME = "apkagent";
    final static String VERSION = "2.4.1";
    private static final String TAG = "apkagent";

    public static void main(String[] args){
        Options options = new Options();
        options.addOption("v", "version", false, "show current version");
        options.addOption("h", "help", false, "show this message");
        options.addOption("s", "size", true, "minicap size");
        options.addOption("q", "quality", true, "minicap quality");
        options.addOption("f", "fps", true, "minicap fps");

        CommandLineParser parser = new DefaultParser();
        HelpFormatter formatter = new HelpFormatter();
        CommandLine cmd;

        try {
            cmd = parser.parse(options, args);
        } catch (ParseException e) {
            System.err.println(e.getMessage());
            formatter.printHelp(PROCESS_NAME, options);
            System.exit(1);
            return;
        }

        if (cmd.hasOption("help")) {
            formatter.printHelp(PROCESS_NAME, options);
            return;
        }
        if (cmd.hasOption("version")) {
            Log.v(TAG, VERSION);
            return;
        }

        int size = 800;
        String str = cmd.getOptionValue("size");
        if(str != null) {
            try {
                size = Integer.parseInt(str);
                if(size < 50) size = 50;
                else if(size > 5000) size = 5000;
            } catch (NumberFormatException e) {
                Log.e(TAG, "size format error: " + size);
            }
        }

        int quality = 80;
        str = cmd.getOptionValue("quality");
        if(str != null) {
            try {
                quality = Integer.parseInt(str);
                if(quality < 50) quality = 50;
                else if(quality > 100) quality = 100;
            } catch (NumberFormatException e) {
                Log.e(TAG, "quality format error: " + str);
            }
        }

        int fps = 20;
        str = cmd.getOptionValue("fps");
        if(str != null) {
            try {
                fps = Integer.parseInt(str);
                if(fps < 2) fps = 2;
                else if(fps > 50) fps = 50;
            } catch (NumberFormatException e) {
                Log.e(TAG, "fps format error: " + str);
            }
        }

        new Console().listenAndServe(size, quality, fps);
    }

    private void listenAndServe(int size, int quality, int maxFPS) {
        Looper.prepare();
        Handler handler = new Handler();

        WindowManagerWrapper wm = new WindowManagerWrapper();
        Point p = wm.getDisplaySize();
        if (p == null){
            System.err.println("Can not get device resolution");
            System.exit(1);
        }

        MinitouchAgent minitouch = new MinitouchAgent(handler);
        MinicapAgent minicap = new MinicapAgent(size, quality, maxFPS);
        RotationAgent rotation = new RotationAgent();
        minitouch.start();
        minicap.start();
        rotation.start();

        Looper.loop();
    }
}
